package ai.chat2db.server.web.start.config.listener.kafaka;

import ai.chat2db.server.domain.api.model.User;
import ai.chat2db.server.domain.api.param.user.UserCreateParam;
import ai.chat2db.server.domain.api.param.user.UserUpdateParam;
import ai.chat2db.server.domain.api.service.UserService;
import ai.chat2db.server.domain.repository.enums.LoginTypeEnum;
import ai.chat2db.server.web.api.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import static com.alibaba.fastjson.JSON.parseObject;

/**
 * @author Asu
 * @version 1.0
 * @date 2024/5/24 11:29
 * @desc 描述
 */
@Component
public class KfkConsumerListener {

    private static final String USER_REGISTER_TOPIC = "mq_mainconsole_user_register";

    private static final String USER_PERMISSION_UPDATE_TOPIC = "mq_mainconsole_user_permission_update";

    private static final String USER_ABANDON_TOPIC = "mq_mainconsole_user_abandon";

    private static final String APPID = "c1eca09674ab45e2a56cf82cff36ea7d";


    @Resource
    private UserService userService;

    @KafkaListener(topics = {USER_ABANDON_TOPIC, USER_PERMISSION_UPDATE_TOPIC, USER_ABANDON_TOPIC}, groupId = "ambari")
    public void onMessage(ConsumerRecord<String, String> data) {
        String topic = data.topic();

        //获取值
        String value = data.value();
        System.out.println("topic = " + data.topic() + " -------value = " + value);
        if (StringUtils.isEmpty(value)) {
            return;
        }

        if (USER_REGISTER_TOPIC.equals(topic)) {
            //转化消息为json
            JSONObject jsonObject = parseObject(value);
            // 用户同步
            String username = jsonObject.getString("username");
            if (StringUtils.isEmpty(username)) {
                return;
            }
            String status = jsonObject.getString("status");
            // 查询用户是否存在
            User user = userService.query(username, LoginTypeEnum.CENTER.getLoginType()).getData();
            if (user == null) {
                // create user
                UserCreateParam userCreateParam = new UserCreateParam();
                userCreateParam.setUserName(username);
                // todo 密码后续进行加密处理
                userCreateParam.setPassword("dizuo@123");
                userCreateParam.setStatus(status);
                userCreateParam.setRoleCode("CLUSTER.USER");
                userCreateParam.setLoginType(LoginTypeEnum.CENTER.getLoginType());
                userService.create(userCreateParam);
            }
        }

        if (USER_PERMISSION_UPDATE_TOPIC.equals(topic)) {
            //转化消息为json
            JSONObject jsonObject = parseObject(value);
            // 用户权限同步
            JSONArray jsonArray = jsonObject.getJSONArray(APPID);
            if (jsonArray == null) {
                return;
            }
            for (Object o : jsonArray) {
                JSONObject one = parseObject(o.toString());
                String username = one.getString("username");
                if (StringUtils.isEmpty(username)) {
                    return;
                }
                //  更新权限表
                JSONArray per = one.getJSONArray("permissions");
                for (Object pr : per) {
                    User user = userService.query(username, LoginTypeEnum.CENTER.getLoginType()).getData();
                    if (user == null) {
                        continue;
                    }
                    UserUpdateParam param = new UserUpdateParam();
                    param.setId(user.getId());
                    param.setRoleCode(pr.toString());
                    userService.update(param);
                }
            }
        }

        if (USER_ABANDON_TOPIC.equals(topic)) {
            //value转为json
            JSONArray jsonArray = JSON.parseArray(value);
            for (Object o : jsonArray) {
                JSONObject jsonOne = parseObject(o.toString());
                //获取删除的username
                String userName = jsonOne.getString("userName");
                User user = userService.query(userName, LoginTypeEnum.CENTER.getLoginType()).getData();
                if (user == null) {
                    return;
                }
                userService.delete(user.getId());
            }
        }
    }
}
